<%--                                                                       --%>
<%-- Copyright (c) 2001-2004 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries.                       --%>
<%--                                                                       --%>

<%--
Important usage note:
Requirements:
  1. Add the following property in the bean used in the EDIT/NEW page:
     
      <form-property name="attContainer" type="com.broadvision.portal.foundation.action.microsite.attachment.AttachmentsContainer" /> 
  
  2. Define two action mappings for adding attachments and deleting attachments. For example:
     <action path="/cs/meetingAttachmentAdd"
        type="com.broadvision.portal.foundation.action.microsite.meeting.MeetingReloadAction"
        name="csMeetingEditForm" parameter="addAttachment" scope="request">
        <exception key="ep.error.microsite.access.permission" type="com.broadvision.portal.services.InsufficientPermissionException" path="/cs/microsite/error.jsp" />
        <forward name="success" path=".microsite.meetingEdit" />
    </action>

    <action path="/cs/meetingAttachmentDelete"
        type="com.broadvision.portal.foundation.action.microsite.meeting.MeetingReloadAction"
        name="csMeetingEditForm" parameter="deleteAttachment" scope="request">
        <exception key="ep.error.microsite.access.permission" type="com.broadvision.portal.services.InsufficientPermissionException" path="/cs/microsite/error.jsp" />
        <forward name="success" path=".microsite.meetingEdit" />
    </action>
    
  3. a. Handle attachments in new page
        In the corresponding action class, add the following method:
     	
     	protected void prepareAttachmentsContainer(ActionMapping mapping, ActionForm form, 
     		HttpServletRequest request, DocumentContext context) throws Exception {
       		AttachmentsContainer containerForm = new AttachmentsContainer();
       		PropertyUtils.setSimpleProperty(form, AttachmentActionLogic.ATTACHMENTS_CONTAINER_NAME, containerForm);
     	}    
  		
     b. Handle attachments in edit page 
     	In the corresponding action class, add the following method:
     	
     	protected void prepareAttachmentsContainer(ActionMapping mapping, ActionForm form, 
     	HttpServletRequest request, DocumentContext context) throws Exception {
       		AttachmentsContainer containerForm = AttachmentsContainer.getInstance(object_Attached_To);
       		PropertyUtils.setSimpleProperty(form, AttachmentActionLogic.ATTACHMENTS_CONTAINER_NAME, containerForm);
     	}
     	
     	Note that the difference between the above two methods is the way to get the AttachmentsContainer instance.

  4. Create a new action class to handle adding and deleting attachments. 
     Besides the regular work, it should have a method like the following one:
      
      protected void prepareAttachmentsContainer(ActionMapping mapping, ActionForm form, 
         HttpServletRequest request, ChecklistTaskContext context) throws Exception {
    
        AttachmentActionLogic.updateAttachmentsList(mapping, form, request, context);
      }
            
  5. Call method "prepareAttachmentsContainer" in the action classes mentioned in previous steps.

  
  Note: For attachmentAddAction, after the action finishes, it can only forward 
        to a jsp page, you cannot forward it to any other .do that needs struts 
        action to automatically populate your form bean. This is because file 
        uploading will use form encoding type: multipart/form-data, and its
        data can only be read once by Struts action servlet. 
        (This is not a Struts bug.)
     
       
  7. Define the following client side global JavaScript variables:
     a. var nextPage = absolutePath of "add_attachment_handler.do";
     b. var attachmentDeleteAction = '<html-ae:rewrite page="delete_attachment_handler.do" />';
     c. Define the following JavaScript variable in your caller jsp also.
        var nextActionAfterCancel = 'something.do'; 
          //Don't use <html-ae:rewrite/> tag here.
        Note: 
          i. something.do should be the action /cs/cancelClearAttachment.do 
             forwards to after it successfully clears all temparary uploaded 
             files.
          ii. For the cancel button in the jsp page, assign its onclick event 
              like the following:
             
               onclick="return cancelClearAttachment(this.form)"
          iii. For editing an existing one and a new one, if after cancellation, 
               you want to let them go to different page, in your caller JSP, 
               you may need use some logic to decide what value to assign to 
               this variable.
     d. Define the following JavaScript variable in the jsp file:
        var nextActionAfterReset = 'something.do'; //Don't use <html-ae:rewrite/> tag here.
        Note: 
          i. something.do should be the action /cs/cancelClearAttachment.do 
             forwards to after it successfully clears all temparary uploaded 
             files.
          ii. For the reset button, assign its onclick event like the following:
             onclick="return resetAttachment(this.form)"
          iii. For editing an existing one and a new one, if after clearing 
               temparary attachments, you want to let them go to different page,
               in your caller JSP, you may need use some logic to decide what 
               value to assign to this variable.
  
  8. In the jsp file, give your form bean a new reference name: formBeanUsed, 
     and put it into request scope.
     
     <bean-ae:define id="formBeanUsed" name="your_form_bean_name" toScope="request" />
      
--%>

<%-- add -ae to all taglib prefix to avoid conflict with parent / container JSP --%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html-ae" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean-ae" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic-ae" %>

<%@ page import="com.broadvision.portal.foundation.action.microsite.attachment.AttachmentsContainer" %>

<bean-ae:define id="attContainer" name="formBeanUsed" property="attContainer" toScope="page" />
<% String uploadApp = application.getInitParameter("UPLOAD_CONTEXT");
%>

<Script language="JavaScript">

function cancelClearAttachment(theForm) {
  if (confirm('<bean-ae:message key="cs.confirm.cancel.edit"/>')) {
    theForm.action = '<html-ae:rewrite page="/cs/attachmentsClearTemp.do"/>';
    <%-- variable: nextActionAfterCancel should be defined in the caller jsp page. --%>
    theForm.nextActionAfter.value = nextActionAfterCancel;
    theForm.submit();
  } 
  return false;
}

function resetAttachment(theForm) {
  if (confirm('<bean-ae:message key="cs.confirm.attachment.reset"/>')) {
    theForm.action = '<html-ae:rewrite page="/cs/attachmentsClearTemp.do"/>';
    <%-- variable: nextActionAfterReset should be defined in the caller jsp page. --%>
    theForm.nextActionAfter.value = nextActionAfterReset;
    theForm.submit();
  } 
  return false;
}

function onClickDeleteAttachment(theForm, index) {
  if ( confirm('<bean-ae:message key="cs.confirm.delete.attachment"/>')) {
    <%-- variable: attachmentDeleteAction should be defined in the caller jsp page. --%>
    theForm.action = attachmentDeleteAction;
    theForm.method = "post";
    theForm.elements["attContainer.indexToDelete"].value = index;
    return true;
  }
  return false;
}            

function onClickAdd(theForm) {
  if (validateAttachment(theForm)) {
    <%-- variable: nextPage should be defined in the caller jsp page. --%>
    theForm.encoding = "multipart/form-data";
    
    theForm.action = '/<%= uploadApp %>'+'/upload.jsp';
    theForm.nextPage.value = nextPage;
    theForm.method = "post";
    return true;
  }
  return false;
}

function validateAttachment(theForm) {
   var attachName = stringTrim(theForm.elements["attName"].value);
   var attachFile = stringTrim(theForm.elements["attOriginalFileName"].value);

   if (attachFile == "") {
      alert('<bean-ae:message key="cs.error.attachment.file"/>');
      return false;
   }

   //
   // File name can not begin with "#" sign.
   //
   if ( navigator.appVersion.indexOf("Win") != -1 ) {
     // browser on window
     var pathArray = attachFile.split("\\");
   } else {
     var pathArray = attachFile.split("/");
   }
   var pathLen = pathArray.length;
   var fileName = pathArray[pathLen - 1];

   if ( fileName.substring(0,1) == "#" ) {
      alert('<bean-ae:message key="cs.error.attachment.file.name"/>');
      return false;
   }

   if (attachName.length > 80) {
      alert("<bean-ae:message key="cs.error.attachment.name.too.long"/>");
      return false;
   }

   if (fileName.length > 80) {
      alert("<bean-ae:message key="cs.error.attachment.filename.too.long"/>");
      return false;
   }

   
   return true;
}    

function downloadAttachment(linkObj, attachmentId) 
{
  var url ='<html-ae:rewrite page="/cs/attachmentDownload.do" name="micrositeNaviInfoView" property="urlParams" />';
  url += "&attId=" + attachmentId;
 
  linkObj.href = url;
  return true; 
}

</Script>
<%
  // In JBoss, there is a local variable called attContainer from <bean-ae:define>
  // above. But, this is missing in Weblogic. Therefore, we need to get the
  // value from the page scope and assign it to a different variable.
  AttachmentsContainer attContainer2 =  (AttachmentsContainer)pageContext.getAttribute("attContainer");
  Object attachments = attContainer2.getAttachments();
  pageContext.setAttribute("attachments", attachments);
%>
<bean-ae:size id="attachmentsCount" name="attachments" />
<input type="hidden" name="attContainer.indexToDelete">
<logic-ae:notEmpty name="attContainer" property="deletedOID">
  <logic-ae:iterate id="deletedOID" name="attContainer" property="deletedOID" indexId="index">
    <input type="hidden" name='attContainer.deletedOID'
        value='<bean-ae:write name="deletedOID" />' >
  </logic-ae:iterate>
</logic-ae:notEmpty>

<%-- hidden field indicating the next action/jsp after clicking
     cancel/reset button. --%>
<input type="hidden" name="nextActionAfter">
<%-- next action after finishing uploading --%>
<input type="hidden" name="nextPage"> 
                           

<logic-ae:greaterThan name="attachmentsCount" value="0">
  <div class="pageSubsubtitle">
    <span class="left"><bean-ae:message key="cs.attachment.attachments.list" /></span>
    <span class="right">&nbsp;</span>
  </div> 
  <table class="dataTable" border="0" cellspacing="1" cellpadding="2" summary="Attachments list" width="100%">
    <tr class="listHeader"> 
      <th id="Filename" align="left"><bean-ae:message key="cs.name" /></th>
      <th id="Actions" align="left"><bean-ae:message key="cs.actions" /></th>
    </tr>
    <logic-ae:iterate id="attachment" name="attachments" 
         type="com.broadvision.portal.foundation.action.microsite.attachment.AttachmentDTO"
         indexId="index">
      <TR class='<%=index.intValue() % 2 == 0 ? "listEven" : "listOdd"%>'>
        <TD headers="Filename" align="left">
          <html-ae:img page="/cs/images/icons/cicon_attachgeneric.gif" border="0" width="22" height="18" border="0" altKey="cs.attachment" titleKey="cs.attachment" align="absmiddle" />
          <logic-ae:greaterThan name="attachment" property="id" value="0">
            <a href="#" onclick="return downloadAttachment(this, <bean-ae:write name="attachment" property="id" />)"><bean-ae:write name="attachment" property="name" /></a>
          </logic-ae:greaterThan>
          <logic-ae:equal name="attachment" property="id" value="0">
            <bean-ae:write name="attachment" property="name" />
          </logic-ae:equal>
          <bean-ae:write name="attachment" property="formattedFileSize" />
          <%-- Since we'll not retrieve data from database again during these operations, we need save
               each DTO's each property as hidden field here. --%>
          <input type=hidden name='attContainer.attachments[<bean-ae:write name="index" />].id' value="<bean-ae:write name="attachment" property="id" />">
          <input type=hidden name='attContainer.attachments[<bean-ae:write name="index" />].name' value="<bean-ae:write name="attachment" property="name" />">
          <input type=hidden name='attContainer.attachments[<bean-ae:write name="index" />].formattedFileSize' value="<bean-ae:write name="attachment" property="formattedFileSize" />">
          <input type=hidden name='attContainer.attachments[<bean-ae:write name="index" />].filePath' value="<bean-ae:write name="attachment" property="filePath" />">
          <logic-ae:equal name="attachment" property="id" value="0">
            <input type=hidden name='attContainer.attachments[<bean-ae:write name="index" />].originalFileName' value="<bean-ae:write name="attachment" property="originalFileName" />">
          </logic-ae:equal>
        </TD>
        <TD headers="Actions" align="left">
          <input type="image" border="0" 
            onclick='return onClickDeleteAttachment(this.form, <bean-ae:write name="index" />)' 
            src="<%=request.getContextPath()%>/cs/images/action/icon_delete.gif"
            alt="<bean:message key="cs.image.alt.delete"/>"
            title="<bean:message key="cs.image.alt.delete"/>">
        </TD>
      </TR>
    </logic-ae:iterate>
  </table>
</logic-ae:greaterThan>

<!-- Begin of adding more attachment section. -->
<div class="pageSubsubtitle">
  <span class="left"><bean-ae:message key="cs.attachment.add.attachment" /></span>
  <span class="right">&nbsp;</span>
</div>

<table class="labelForm" width="100%" border="0" cellspacing="0" cellpadding="0" summary="Add attachment table">
  <tr>
    <Td class="label" width="10%">
      <bean-ae:message key="cs.name" />:
    </td>
    <td class="labelField" width="90%">
      <input type="text" name="attName"> <%-- attachment file friendly name --%>
    </td>
  </tr>
  <tr>
    <Td class="label" width="10%">
      <bean-ae:message key="cs.file" />:
    </td>
    <td class="labelField" width="90%">
      <input type="file" name="attOriginalFileName">&nbsp; <%-- actual orignal name --%>
      <html-ae:submit property="addMoreBtn" styleClass="inputButton" 
        titleKey="button.upload" onclick="return onClickAdd(this.form)">
        <bean-ae:message key="button.upload"/>
      </html-ae:submit>
     </td>
  </tr>
</table>
<!-- end of adding more attachment section. -->
